package com.castle.fortress.admin.flowable.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.flowable.dto.FlowableAppDto;
import com.castle.fortress.admin.flowable.dto.FlowableTemplateDto;
import com.castle.fortress.admin.flowable.entity.FlowableTemplateEntity;
import com.castle.fortress.admin.flowable.mapper.FlowableTemplateMapper;
import com.castle.fortress.admin.flowable.service.ApprovesService;
import com.castle.fortress.admin.flowable.service.FlowableAppService;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.common.utils.ConvertUtil;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 审批管理 服务实现类
 *
 * @author castle
 * @since 2022-09-21
 */
@Service
public class ApprovesServiceImpl extends ServiceImpl<FlowableTemplateMapper, FlowableTemplateEntity> implements ApprovesService {

    @Qualifier("processEngine")
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private TaskService taskService;

    @Autowired
    private FlowableAppService flowableAppService;
    /**
    * 获取查询条件
    * @param flowableTemplateDto
    * @return
    */
    public QueryWrapper<FlowableTemplateEntity> getWrapper(FlowableTemplateDto flowableTemplateDto){
        QueryWrapper<FlowableTemplateEntity> wrapper= new QueryWrapper();
        if(flowableTemplateDto != null){
            FlowableTemplateEntity flowableTemplateEntity = ConvertUtil.transformObj(flowableTemplateDto,FlowableTemplateEntity.class);
            wrapper.eq(flowableTemplateEntity.getId() != null,"id",flowableTemplateEntity.getId());
            wrapper.like(StrUtil.isNotEmpty(flowableTemplateEntity.getTitle()),"title",flowableTemplateEntity.getTitle());
            wrapper.eq(flowableTemplateEntity.getStatus() != null,"status",flowableTemplateEntity.getStatus());
        }
        return wrapper;
    }


    @Override
    public IPage<FlowableTemplateDto> pageApproves(Page<FlowableTemplateDto> page) {
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();
        List<FlowableTemplateDto> pdResult = new ArrayList<>();
        for (ProcessDefinition processDefinition : list) {
            FlowableTemplateDto dto = new FlowableTemplateDto();
            dto.setActKey(processDefinition.getKey());
            dto.setTitle(processDefinition.getName());
            dto.setDeployId(processDefinition.getDeploymentId());
            pdResult.add(dto);
        }
        Page<FlowableTemplateDto> pageDto = new Page(page.getCurrent(), page.getSize(),pdResult.size(),false);
        pageDto.setRecords(pdResult);
        return pageDto;
    }


    @Override
    public List<FlowableTemplateDto> listFlowableTemplate(FlowableTemplateDto flowableTemplateDto){
        QueryWrapper<FlowableTemplateEntity> wrapper = getWrapper(flowableTemplateDto);
        List<FlowableTemplateEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,FlowableTemplateDto.class);
    }


    @Override
    public IPage<FlowableAppDto> pageFlowableApp(Page<FlowableAppDto> page, FlowableAppDto flowableAppDto, SysUser user) {
        return flowableAppService.pageWithAuth(page,flowableAppDto,user);
    }
}

